﻿using ManyToManyAgain.Data;
using ManyToManyAgain.Model;
using ManyToManyAgain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading.Tasks;
using System.Collections;
namespace ManyToManyAgain.Controllers
{
    public class HomeController : Controller
    {
        List<HomeIndexViewModel> Albums;


        public ActionResult Index()
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Albums = db.Albums.Select(c => new HomeIndexViewModel()
                {
                    AlbumId = c.AlbumId,
                    Title = c.Title,
                    Artist = c.Artist,
                    AlbumSongs = c.Albums_Songs.Select(s => new SongViewModel()
                    {
                        SongId = s.Song.SongId,
                        Title = s.Song.Title,
                        DateReleased = s.Song.DateReleased
                    }).ToList()
                }).ToList();
                return View(Albums);
            }
        }
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ActionResult AddBook()
        {

            return View(new Song());

        }
        [HttpPost]
        public ActionResult AddSong(Song model) 
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model.SongId = db.AlbumSongs.OrderBy(x => x.SongId).LastOrDefault().SongId + 1;
                db.Songs.Add(model);

            };
            return RedirectToAction("Index");  
        } 

    }
}